// components/search/index.js
import moment from 'moment';
import {
  list,
  search
} from '../../api/number'

import {
  searchData
} from '../../dataSource';
const sd = JSON.parse(JSON.stringify(searchData));
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    params: {
      type: Object,
      value: {
        ...searchData
      }
    },
    name: {
      type: String,
      value: '名称'
    },
    asc: {
      type: Boolean,
      value: false
    },
    type: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    currentDate: moment().valueOf(),
    minDate: moment().add(-3, 'y').valueOf(),
    maxDate: moment().valueOf(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    datePickerShow: false,
    params: JSON.parse(JSON.stringify(searchData)),
    _params: '',
    searchType: [],
    date: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindDateChange(event) {
      const value = event.detail.value,
        index = event.currentTarget.dataset.index,
        params = this.data.params;
      params['map'][String(index + 1)] = value;
 
      this.setData({
        params,
        date: value
      })
    },
    clearDate(event){
     const index = event.currentTarget.dataset.index,
      params = this.data.params;
    params['map'][String(index + 1)] = null;
      this.setData({
         params,
         date: null
      })
    },
    onTriggerDatePicker(event) {
      const params = event.currentTarget.dataset.params;
      this.setData({
        _params: params
      })
      this.openDatePicker();
    },
    openDatePicker() {
      this.setData({
        datePickerShow: true
      })
    },
    onSwitchChange(event) {
      this.setData({
        params: {
          ...this.data.params,
          asc: event.detail
        }
      });
    },
    onPickerConfirm(event) {
      this.setData({
        currentDate: event.detail,
        params: {
          ...this.data.params,
          [this.data._params]: moment(event.detail).format("YYYY-MM-DD")
        }
      });
      this.closeDatePicker();
    },
    onNameChange(event) {
      const value = event.detail.value,
        params = this.data.params;
      params.name = value;
      this.setData({
        params
      })
    },
    onInputChange(event) {
      const value = event.detail.value,
        index = event.currentTarget.dataset.index,
        params = this.data.params;
      params['map'][String(index + 1)] = value;
      this.setData({
        params
      })
    },
    closeDatePicker() {
      this.setData({
        datePickerShow: false
      })
    },
    onClose() {
      this.triggerEvent('close');
    },
    onReset() {
      this.setData({
        params: JSON.parse(JSON.stringify(sd)),
      })
      if (!this.properties.asc) {
        delete sd.asc
      }
      const coms = this.selectAllComponents('.select');
      coms?.map(it => {
        it.reset();
      })
      this.triggerEvent('search', sd);
    },
    onConfirm() {
      let {
        params
      } = this.data;
      if (!this.properties.asc) {
        delete params.asc
      }
      for (let x in params['map']) {
        params['map'][x] = params['map'][x] === '不限' ? null : params['map'][x]
      }
      this.triggerEvent('search', params);
    },
    fetchSettings() {
      list({
        numSettingsType: this.properties.type
      }, res => {
        this.setData({
          searchType: res
        })
      })
    },
    /**
     * 选择器
     * @param {wx.currentTarget} event 
     */
    onSelect(event) {
      const value = event.detail,
        index = event.currentTarget.dataset.index,
        params = this.data.params;
      params['map'][String(index + 1)] = value;
      this.setData({
        params
      })
    },
  },
  lifetimes: {
    attached() {
      this.fetchSettings();
    }
  }
})